﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Kristall.Engine;

namespace Kristall.Editor.Commands
{
    public class ResizeAnimationCommand : ICommand
    {
        Animatable _animatable;

        public ResizeAnimationCommand(Animatable animatable)
        {
            _animatable = animatable;
        }

        public bool Execute()
        {
            AnimatableSprite animation = _animatable.Animations.FirstOrDefault(x => x.FileName == _animatable.CurrentAnimationKey);

            if (animation != null)
            {
                float width = animation.Texture.Width;
                float height = animation.Texture.Height;

                int scaleY = (int)Math.Ceiling(_animatable.Bounds.Height / height);
                int scaleX = (int)Math.Ceiling(_animatable.Bounds.Width / width);

                _animatable.SetSize((int)(scaleX * width), (int)(scaleY * height));
                animation.FrameWidth = (int)(scaleX * width);
                animation.FrameHeight = (int)(scaleY * height);
                return true;
            }
            return false;
        }

        public bool Undo()
        {
            return true;
        }

        public string Name
        {
            get { return "Resize Animation"; }
        }
    }
}
